import numpy as np
import Config as conf
from sklearn.externals import joblib
from sklearn import svm
from sklearn import linear_model

# constants
LINEAR_MODEL_PATH = conf.CACHE_DIRECTORY_PATH + 'linear.pkl'
SVR_MODEL_PATH = conf.CACHE_DIRECTORY_PATH + 'svr.pkl'

# svm regression
def svr(x_train, y_train, x_test):
    assert len(x_train) == len(y_train)
    clf = svm.SVR()
    clf.fit(x_train, y_train)

    # save model
    joblib.dump(clf, LINEAR_MODEL_PATH)

    return clf.predict(x_test)

# linear regression
def linear(x_train, y_train, x_test):
    clf = linear_model.LinearRegression()
    clf.fit(x_train, y_train)

    # save model
    joblib.dump(clf, LINEAR_MODEL_PATH)

    return clf.predict(x_test)


# vectorized error calc
def rmsle(p, a):
    assert len(p) == len(a)
    return np.sqrt(np.mean(np.power(np.log1p(p)-np.log1p(a), 2)))

# get model
def get_model_by_file(file_path):
    return joblib.load(file_path)